from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.shortcuts import render

from .models import Category, Banner, Article, Tag, Link


# import 分页插件包


def index(request):
    sitename = "Django 网站"
    url = 'www.baidu.com'
    # 所有分类 顶部导航
    allcategory = Category.objects.all()
    # 可用的banner
    banner = Banner.objects.filter(is_active=True)[0:4]
    # 推荐文章
    tui = Article.objects.filter(tui__id=1)[:3]
    # 最新文章取10个
    allarticle = Article.objects.all().order_by('-id')[0:10]
    # allarticle = Article.objects.all()
    # 热门文章排行
    hot = Article.objects.all().order_by('views')[:10]
    # 标签
    tags = Tag.objects.all()
    # 友情链接
    link = Link.objects.all()

    list = [
        '开发前准备',
        '项目需求分析',
        '数据库设计分析',
        '创建项目',
        '基础配置',
        'welcome page',
        'create database model',
    ]
    mydict = {
        'name': '小马哥',
        'qq': '1423808921',
        'wx': 'yi',
        'email': 'edwin.ma@foxmail.com',
        'Q群': '66666666',
    }

    # 上下文封装
    context = {
        'allcategory': allcategory,
        'sitename': sitename,
        'url': url,
        'list': list,
        'mydict': mydict,
        'banner': banner,
        'tui': tui,
        'allarticle': allarticle,
        'hot': hot,
        'tags': tags,
        'link': link,

    }
    return render(request, 'index.html', context)


# 列表页
def list(request, lid):
    list = Article.objects.filter(category_id=lid).order_by("views")  # 获取url 分类id 文章
    cname = Category.objects.get(id=lid)  # 获取文章栏目名称
    remen = Article.objects.filter(tui__id=2)[:6]  # 右侧热门推荐
    allcategory = Category.objects.all()  # 导航所有分类
    tags = Tag.objects.all()  # 右侧标签分类
    hot = Article.objects.all().order_by("views")[:10]
    page = request.GET.get('page')
    paginator = Paginator(list, 5)  # 查询数据对象分页
    try:
        list = paginator.page(page)
    except PageNotAnInteger:
        list = paginator.page(1)  # 输入页数 不是数字，到第一页
    except EmptyPage:
        list = paginator.page(paginator.num_pages)  # 输入不在，最后一页
    return render(request, 'list.html', locals())


# 内容页
def show(request, sid):
    show = Article.objects.get(id=sid)  # search article by id
    # print(show.title,show.body)
    allcategory = Category.objects.all()
    tags = Tag.objects.all()  # 右侧标签页
    remen = Article.objects.filter(tui__id=2)[:6]  # 右侧热门推荐
    hot = Article.objects.all().order_by('?')[:10]  # 随机推荐
    previous_blog = Article.objects.filter(created_time__gt=show.created_time, category=show.category.id).first()
    next_blog = Article.objects.filter(created_time__lt=show.created_time, category=show.category.id).last()
    show.views = show.views + 1
    show.save()
    return render(request, 'show.html', locals())


# 标签页
def tag(request, tag):
    list = Article.objects.filter(tags__name=tag)
    remen = Article.objects.filter(tui__id=2)[:6]
    allcategory = Category.objects.all()
    tname = Tag.objects.get(name=tag)
    page = request.GET.get('page')
    tags = Tag.objects.all()
    paginator = Paginator(list, 5)
    try:
        list = paginator.page(page)  # 当前页码
    except PageNotAnInteger:
        list = paginator.page(1)
    except EmptyPage:
        list = paginator.page(paginator.num_pages)
    return render(request, 'tags.html', locals())


# 搜索页
def search(request):
    pass


# 关于我们
def about(request):
    return render(request, 'about.html')
